
package com.us.install;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class NetServlet extends HttpServlet {

    public static Map<String, String> content = new HashMap<String, String>();

    private static final long serialVersionUID = 706445275899838742L;

    private static Map<String, String[]> LINK_STATIC = new HashMap<String, String[]>();

    static {
        String[] mysql = { "com.mysql.jdbc.Driver", "jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=utf8" };
        String[] sqlserver = { "com.microsoft.sqlserver.jdbc.SQLServerDriver", "jdbc:sqlserver://%s:%s;databaseName=%s" };
        String[] oracle = { "oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@%s:%s:%s" };
        LINK_STATIC.put("mysql", mysql);
        LINK_STATIC.put("sqlserver", sqlserver);
        LINK_STATIC.put("oracle", oracle);
    }

    // ping的路径
    public static boolean ping(String host, String port, String dbname, String username, String passwd, String dbtype)
                                                                                                                      throws Exception{
        Class.forName(LINK_STATIC.get(dbtype)[0]);
        String link = String.format(LINK_STATIC.get(dbtype)[1], host, port, "mysql");
        Connection connect = DriverManager.getConnection(link, username, passwd);
        return connect != null;
    }

    @Override
    protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
                                                                                   throws ServletException,
                                                                                   IOException{
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        String host = request.getParameter("host");
        String port = request.getParameter("port");
        String dbname = request.getParameter("dbname");
        String username = request.getParameter("username");
        String passwd = request.getParameter("passwd");
        String dbtype = request.getParameter("dbtype");
        String copyright = request.getParameter("copyright");
        String createDb = request.getParameter("createDb");
        // String sequence = request.getParameter("sequence");
        if (host != null) {
            try {
                boolean conected = ping(host, port, dbname, username, passwd, dbtype);
                if (conected) {
                    content.put("db.host", host.trim());
                    content.put("db.port", port.trim());
                    content.put("db.name", dbname.trim());
                    content.put("db.user", username.trim());
                    content.put("db.passwd", passwd.trim());
                    content.put("db.createnew", createDb.trim());
                    content.put("cr", copyright);
                    content.put("db.createurl", String.format("jdbc:mysql://%s:%s/", host, port));
                    response.getWriter().write("SUC");
                }
            }
            catch (Exception e) {
                e.printStackTrace();
                response.getWriter().write("连接失败:" + e.getMessage());
            }
        }
    }
}
